package com.ss.android.article.base.feature.subscribe.activity;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.ColorFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.frameworks.baselib.network.http.util.TaskInfo;
import com.ss.android.account.b.a.b.b$a;
import com.ss.android.account.model.BaseUser;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.app.c.ImageManager;
import com.ss.android.article.base.feature.pgc.PgcActivity;
import com.ss.android.article.base.feature.subscribe.b.SubscribeManager;
import com.ss.android.article.base.feature.subscribe.b.f;
import com.ss.android.article.base.feature.subscribe.model.EntryItem;
import com.ss.android.article.base.feature.subscribe.model.SubscribeData;
import com.ss.android.article.base.feature.subscribe.model.b;
import com.ss.android.article.browser.R;
import com.ss.android.common.app.AbsFragment;
import com.ss.android.common.app.LifeCycleMonitor;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.image.AvatarLoader;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public final class c extends AbsFragment implements b$a, SubscribeManager.SubscribeDataChangeListener {
    public static final class c$a extends BaseAdapter implements LifeCycleMonitor {
        final class c$a$a {
            public ImageView a;
            public TextView b;
            public TextView c;
            public ImageView d;
            public SubscribeButton e;

            c$a$a(byte arg1) {
                this();
            }

            private c$a$a() {

            }
        }

        List a;
        private ImageManager b;
        private AvatarLoader c;
        private TaskInfo d;
        private Resources e;

        public c$a(Context arg9) {
            this.a = new ArrayList();
            this.b = new ImageManager(arg9);
            this.d = new TaskInfo();
            int v6 = arg9.getResources().getDimensionPixelSize(R.dimen.i6);
            int v4 = arg9.getResources().getDimensionPixelSize(R.dimen.i9);
            this.c = new AvatarLoader(com.ss.android.article.browser.R.drawable.m4, this.d, this.b, v4, new b(v4, v6, arg9.getResources().getDimensionPixelSize(R.dimen.i8)));
            this.e = arg9.getResources();
        }

        public static void a(long arg8, ListView arg10, int arg11) {
            if (arg8 >= 0 && arg10 != null) {
                int v3 = arg10.getChildCount();
                int v2;
                for (v2 = 0; v2 < v3; ++v2) {
                    Object v0 = arg10.getChildAt(v2).getTag();
                    if (!(v0 instanceof c$a$a)) {
                        v0 = null;
                    }

                    if (v0 != null && ((c$a$a) v0).e != null) {
                        final SubscribeButton v4 = ((c$a$a) v0).e;
                        if (v4.b != null && v4.b.getUserId() == arg8) {
                            if (arg11 == 0 || arg11 == 1009) {
                                EntryItem v5 = v4.b;
                                boolean v0_1 = !v4.b.isSubscribed() ? true : false;
                                v5.setSubscribed(v0_1);
                            }

                            v4.a(v4.b.isSubscribed(), v4.b.mIsLoading);
                            new Thread(new Runnable() {

                                public final void run() {
                                    try {
                                        DBHelper.getInstance(v4.getContext())
                                                .a(SubscribeButton.a(v4),
                                                        SubscribeButton.a(v4).
                                                                isSubscribed());
                                    } catch (Exception exception) {
                                    }
                                }
                            }).start();
                            SubscribeManager.getInstance().c.a(v4.b);
                        }
                    }
                }
            }
        }

        public static void a(long arg6, ListView arg8) {
            if (arg6 > 0 && arg8 != null) {
                int v2 = arg8.getChildCount();
                int v1;
                for (v1 = 0; v1 < v2; ++v1) {
                    Object v0 = arg8.getChildAt(v1).getTag();
                    if (!(v0 instanceof c$a$a)) {
                        v0 = null;
                    }

                    if (v0 != null && ((c$a$a) v0).e != null) {
                        SubscribeButton v0_1 = ((c$a$a) v0).e;
                        if (v0_1.b != null && v0_1.b.mId == arg6) {
                            v0_1.a(v0_1.b.isSubscribed(), v0_1.b.mIsLoading);
                        }
                    }
                }
            }
        }

        public final EntryItem a(int arg2) {
            return (EntryItem) this.a.get(arg2);
        }

        public final void onDestroy() {
            if (this.c != null) {
                this.c.onDestroy();
            }
        }

        public final void onResume() {
            if (this.c != null) {
                this.c.onPause();
            }
        }

        public final void onPause() {
        }

        public final int getCount() {
            return this.a.size();
        }

        public final Object getItem(int arg2) {
            return this.a(arg2);
        }

        public final long getItemId(int arg3) {
            return ((long) arg3);
        }

        public final View getView(int arg6, View arg7, ViewGroup arg8) {
            if (arg7 == null) {
                arg7 = LayoutInflater.from(arg8.getContext()).inflate(com.ss.android.article.browser.R.layout.d1, arg8, false);
                c$a$a v1 = new c$a$a((byte) 0);
                v1.a = arg7.findViewById(com.ss.android.article.browser.R.id.icon);
                v1.b = arg7.findViewById(com.ss.android.article.browser.R.id.entry_name);
                v1.c = arg7.findViewById(com.ss.android.article.browser.R.id.sj);
                v1.e = arg7.findViewById(com.ss.android.article.browser.R.id.subscribe_btn);
                v1.d = arg7.findViewById(com.ss.android.article.browser.R.id.divider);
                arg7.setTag(v1);
            }

            Object v0 = this.a.get(arg6);
            Object v1_1 = arg7.getTag();
            this.c.loadAvatar(((c$a$a) v1_1).a, ((EntryItem) v0).mIconUrl);
            ((c$a$a) v1_1).b.setText(((EntryItem) v0).mName);
            TextView v3 = ((c$a$a) v1_1).c;
            String v2 = android.support.design.a.isTextEmpty(((EntryItem) v0).mDescription) ? " " : ((EntryItem) v0).mDescription;
            v3.setText(((CharSequence) v2));
            SubscribeButton v2_1 = ((c$a$a) v1_1).e;
            v2_1.setTag(v0);
            v2_1.a(((EntryItem) v0).isSubscribed(), ((EntryItem) v0).mIsLoading);
            v2_1.b = ((EntryItem) v0);
            AppData.getInstance();
            boolean v0_1 = AppData.isNightMode();
            arg7.setBackgroundResource(com.ss.android.article.browser.R.drawable.e5);
            if (v1_1 != null) {
                ColorFilter v0_2 = v0_1 ? TTUtils.getNightModeColorFilter() : null;
                ((c$a$a) v1_1).a.setColorFilter(v0_2);
                ((c$a$a) v1_1).b.setTextColor(this.e.getColor(com.ss.android.article.browser.R.color.entry_subscribe_list_item_name));
                ((c$a$a) v1_1).c.setTextColor(this.e.getColor(com.ss.android.article.browser.R.color.entry_subscribe_list_item_desc));
                ((c$a$a) v1_1).d.setBackgroundColor(this.e.getColor(com.ss.android.article.browser.R.color.divider));
                SubscribeButton v0_3 = ((c$a$a) v1_1).e;
                Resources v1_2 = this.e;
                v0_3.a.setBackgroundResource(com.ss.android.article.browser.R.drawable.subscribe_btn_bg);
                v0_3.a.setTextColor(v1_2.getColorStateList(com.ss.android.article.browser.R.color.subscribe_btn_text));
            }

            return arg7;
        }

        public final void onStop() {
            if (this.c != null) {
                this.c.onStop();
            }
        }
    }

    c$a a;
    private ListView b;
    private long c;

    public c() {

    }

    public final void a(int arg4, int arg5, BaseUser arg6) {
        if ((arg5 == 100 || arg5 == 101) && (arg6 != null && this.a != null && this.b != null)) {
            c$a.a(arg6.mUserId, this.b, arg4);
        }
    }

    public final void a(BaseUser arg1) {
    }

    public final void onSubscribeDataChanged(SubscribeData arg5) {
        Object v0_1;
        if (this.isViewValid()) {
            if (arg5.a == 4) {
                if (this.c <= 0) {
                    return;
                }

                if (this.c != arg5.d) {
                    return;
                }

                if (!(arg5.c instanceof List)) {
                    return;
                }

                try {
                    v0_1 = arg5.c;
                    c$a v1 = this.a;
                    v1.a.clear();
                    v1.a.addAll(((Collection) v0_1));
                    v1.notifyDataSetChanged();
                } catch (Exception v0) {
                }

                return;
            }

            if (arg5.a == 3) {
                v0_1 = (arg5.c instanceof EntryItem) ? arg5.c : null;
                if (v0_1 == null) {
                    return;
                }

                if (this.a == null) {
                    return;
                }

                if (this.b == null) {
                    return;
                }

                c$a.a(((EntryItem) v0_1).mId, this.b);
            }
        }
    }

    public final void onActivityCreated(Bundle arg5) {
        super.onActivityCreated(arg5);
        Bundle v0 = this.getArguments();
        if (v0 != null) {
            this.c = v0.getLong("group_id");
        }

        this.a = new c$a(this.getActivity());
        this.b.setAdapter(this.a);
        this.registerLifeCycleMonitor(this.a);
        SubscribeManager.getInstance().addSubscribeDataChangeListener(this);
        com.ss.android.account.b.a.b.a(this.getActivity()).a(this);
        this.b.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public final void onItemClick(AdapterView arg5, View arg6, int arg7, long arg8) {
                FragmentActivity fragmentActivity = c.this.getActivity();
                if (fragmentActivity != null && c.this.a != null) {
                    try {
                        MobClickCombiner.onEvent(fragmentActivity, "subscription", "enter_pgc");
                        PgcActivity.startActivity(fragmentActivity, c.this.a.a(arg7).mId, "channel_subscription_category");
                    } catch (Exception v0_1) {
                    }
                }
            }
        });
        if (this.c > 0) {
            SubscribeManager v0_1 = SubscribeManager.getInstance();
            long v2 = this.c;
            SubscribeManager.checkUIThread();
            new f(v0_1.c, v2).start();
        }
    }

    public final View onCreateView(LayoutInflater arg3, ViewGroup arg4, Bundle arg5) {
        View v1 = arg3.inflate(com.ss.android.article.browser.R.layout.d2, arg4, false);
        this.b = v1.findViewById(com.ss.android.article.browser.R.id.sk);
        AppData.getInstance();
        return v1;
    }

    public final void onDestroy() {
        SubscribeManager.getInstance().b(this);
        com.ss.android.account.b.a.b.a(this.getActivity()).b(this);
        super.onDestroy();
    }
}

